package com.stack.knowyouweb.aop;

import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Lang;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * web 接口日志打印切面
 * @author ldy
 */
@Component
@Aspect
@Slf4j
public class WebAspect {

    @Autowired
    private Gson gson;

    @Pointcut("execution(public * com.stack.knowyouweb.controller.*.*(..))")
    public void pointCut(){
    }

    @Around("pointCut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable{
        // 获取执行的controller 名称
        String controllerName = joinPoint.getTarget().getClass().getSimpleName();
        // 获取执行方法名称
        String method = joinPoint.getSignature().getName();
        // 获取参数列表
        String args = Arrays.toString(joinPoint.getArgs());
        log.info("<{}.{}>,请求参数:{}", controllerName, method, args);
        long startTime=System.currentTimeMillis();
        // 执行请求
        Object ret = joinPoint.proceed();
        long endTime=System.currentTimeMillis();
        log.info("<{}.{}>,响应结果:{}", controllerName,method,gson.toJson(ret));
        log.info("<{}.{}>,响应时间:{}毫秒",controllerName,method,(endTime-startTime));
        return ret;
    }


}
